﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： CardContentRelaRepository.cs
// 项目名称：斑马车务
// 创建时间：2019/10/22
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using YouWei.Infrastructure.Entity;
using Dapper.Extensions;
using YouWei.Infrastructure.Models;
using Dapper;

namespace YouWei.Infrastructure.Repository
{
	/// <summary>
	///车主卡福利关联 仓储层
	/// </summary>	
	public class UserCardContentRelaRepository:BaseRepository<UserCardContentRela>
	{		
    
        #region 构造

        public UserCardContentRelaRepository() : base(){}

        public UserCardContentRelaRepository(string connectionString) : base(connectionString){}

        public UserCardContentRelaRepository(DatabaseContext dbContext) : base(dbContext){}

        #endregion

        /// <summary>
        /// 分页查询，带条件
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public Page<UserCardContentRela> GetPages(int pageIndex, long pageSize, ReqCardContentRelaCondition condition)
        {
            StringBuilder sql = new StringBuilder("select t.*,t1.Name as CardName,t2.Name as ContentName from UserCardContentRela t with(nolock) inner join UserCard t1 with(nolock) on t.cardid = t1.CardId inner join UserCardContent t2 with(nolock) on t.CardContentId = t2.CardContentId");
            if (!string.IsNullOrWhiteSpace(condition.StartDate))
            {
                sql.Append(" AND t.CreatedDate>@StartDate");
            }
            if (!string.IsNullOrWhiteSpace(condition.EndDate))
            {
                sql.Append(" AND t.CreatedDate<@EndDate");
            }
            if (condition.CardId > 0)
            {
                sql.Append(" AND t.CardId = @CardId");
            }
            if (condition.CardContentId > 0)
            {
                sql.Append(" AND t.CardContentId = @CardContentId");
            }


            DynamicParameters dparams = new DynamicParameters();
            dparams.Add("@CardId", condition.CardId, System.Data.DbType.Int32);
            dparams.Add("@CardContentId", condition.CardContentId, System.Data.DbType.Int32);
            dparams.Add("@StartDate", condition.StartDate, System.Data.DbType.String);
            dparams.Add("@EndDate", condition.EndDate, System.Data.DbType.String);
            return dbContext.Connection.Page<UserCardContentRela>(pageIndex, pageSize, sql.ToString(), dparams);
        }

        /// <summary>
        /// 获取客户卡内容列表
        /// </summary>
        /// <returns></returns>
        public List<UserCardContentInfo> GetListByCardId(int cardId)
        {
            string sql = @" SELECT  t.*,                                    
                                    t2.Name AS ContentName,
                                    t2.Category,
                                    t2.CardType,
                                    t2.Icon,
                                    t2.price AS ContentPrice,
                                    t2.UrlName,
                                    t2.Url,
                                    t2.DetailUrl AS ContentDetailUrl,
                                    t2.Description AS ContentDescription, 
                                    t2.PayUrl,
                                    t2.PayUrlName
                            FROM    UserCardContentRela t WITH ( NOLOCK )                                  
                                    LEFT JOIN UserCardContent t2 WITH ( NOLOCK ) ON t.CardContentId = t2.CardContentId
                            WHERE   t.CardId = @CardId
                            ORDER BY t.RelaSort ASC";
            DynamicParameters dparams = new DynamicParameters();
            dparams.Add("@CardId", cardId, System.Data.DbType.Int32);
            return dbContext.Connection.Query<UserCardContentInfo>(sql.ToString(), dparams).AsList();
        }


       
    }
}